import { GameConfig } from "./GameConfig"

class Wall {
  THREE
  scene
  meshTemplate
  mesh
  isAlive = true
  constructor(scene) {
    this.scene = scene
  }

  getMesh(){
    return this.mesh
  }

  initTemplate(THREE){
    if(!this.meshTemplate){
      const THREE = this.THREE

      this.meshTemplate = new THREE.Group()
  
      // 1. 创建立方体
      const geometry = new THREE.BoxGeometry(1, 1, 1)
      // 半透明材质
      const material = new THREE.MeshPhongMaterial({
        color: 0xff00ff,
        transparent: true,
        opacity: 0.3,
        side: THREE.DoubleSide // 防止看到空洞
      })
  
      const cube = new THREE.Mesh(geometry, material)
      this.meshTemplate.add(cube)
  
      // 2. 创建高亮边框
      const edges = new THREE.EdgesGeometry(geometry)
      const lineMaterial = new THREE.LineBasicMaterial({ color: 0xffffff, linewidth: 2 })
      const line = new THREE.LineSegments(edges, lineMaterial)    
      this.meshTemplate.add(line)
    }
  }

  init(THREE) {
    this.THREE = THREE
    this.initTemplate();
    this.mesh = this.meshTemplate.clone(true)
    this.scene.add(this.mesh)
  }

  setAlive(isVisible){
    this.isAlive = isVisible;
    this.mesh.visible = isVisible;
  }

  wallEvent
  setEvent(wallEvent){
    this.wallEvent = wallEvent;
    this.setGrid(wallEvent.LineIndex, wallEvent.LineLayer);
    this.setSize(wallEvent.Width, wallEvent.Height, wallEvent.Length);    
  }

  getEvent(){
    return this.wallEvent;
  }

  setGrid(index, layer) {
    this.mesh.position.set(
      ...GameConfig.GetGridPositionXY(index, layer), 0.0
    )
  }

  setSize(width, height, length){
    this.mesh.scale.set(width, height, length)
    console.log("Length:" + length)
  }
}

export {Wall}